package flc.ast.fragment;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SelLineActivity;
import flc.ast.bean.MyLineBean;
import flc.ast.bean.MyProtectionBean;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.IUserSysEvent;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.TimeUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<MyLineBean> listLine = new ArrayList<>();
    private int selPosition, type;
    private boolean isOpen;
    private ObjectAnimator mRotAnimator;
    private Handler mHandler;
    private int mRecordTime;
    private final Runnable mUpdateRecordTimeTask = new Runnable() {
        @Override
        public void run() {
            mRecordTime++;
            mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivOpen.setOnClickListener(this);
        mDataBinding.ivType.setOnClickListener(this);
        mDataBinding.llExchangeLine.setOnClickListener(this);

        mHandler = new Handler();

        mRotAnimator = ObjectAnimator.ofFloat(mDataBinding.myCpvOpen, "rotation", 0f, 360f);
        mRotAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mRotAnimator.setInterpolator(new LinearInterpolator());//插值器实现匀速旋转
        mRotAnimator.setDuration(1000);//时间
    }

    @SuppressLint("ResourceType")
    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivOpen://开启/关闭
                if (isOpen) {
                    isOpen = false;
                    mDataBinding.tvConnect.setText(getString(R.string.not_connect));
                    mDataBinding.myCpvClose.setVisibility(View.VISIBLE);
                    mDataBinding.myCpvOpen.setVisibility(View.GONE);
                    mRotAnimator.cancel();//结束动画
                    stopRecordTime();
                    saveRecord();
                } else {
                    UserSysEventProxy.getInstance().payEventWithFreeNumOfTimes(mActivity, Content.fragment_home_key,
                            Content.fragment_home_max, new IUserSysEvent.IPayEventCallback() {
                                @Override
                                public void onPayOk(boolean isRealOk) {
                                    if (isRealOk) {
                                        isOpen = true;
                                        mDataBinding.tvConnect.setText(getString(R.string.is_connect));
                                        mDataBinding.myCpvClose.setVisibility(View.GONE);
                                        mDataBinding.myCpvOpen.setVisibility(View.VISIBLE);
                                        mRotAnimator.start();//开始动画
                                        startRecordTime();
                                    }
                                }
                            });
                }
                break;
            case R.id.ivType://类型
                if (type == 0) {
                    type = 1;
                    mDataBinding.ivType.setImageResource(R.drawable.zn);
                } else {
                    type = 0;
                    mDataBinding.ivType.setImageResource(R.drawable.bz);
                }
                break;
            case R.id.llExchangeLine://切换线路
                isOpen = false;
                mDataBinding.tvConnect.setText(getString(R.string.not_connect));
                mDataBinding.myCpvClose.setVisibility(View.VISIBLE);
                mDataBinding.myCpvOpen.setVisibility(View.GONE);
                mRotAnimator.cancel();//结束动画
                stopRecordTime();
                saveRecord();
                SelLineActivity.selPosition = selPosition;
                startActivity(SelLineActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name1), false));
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name2), false));
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name3), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name4), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name5), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name6), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name7), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name8), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name9), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name10), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name11), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name12), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name13), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name14), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name15), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name16), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name17), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name18), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name19), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name20), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name21), false));
    }

    @Override
    public void onResume() {
        super.onResume();
        selPosition = SPUtil.getInt(mContext, Content.Line, 0);
        mDataBinding.ivImg.setImageResource(listLine.get(selPosition).getImg());
        mDataBinding.tvName.setText(listLine.get(selPosition).getName());
    }

    private void saveRecord() {
        if (mRecordTime < 1) return;
        List<MyRecordBean> listSave = new ArrayList<>();
        listSave.add(new MyRecordBean(listLine.get(selPosition).getImg(), listLine.get(selPosition).getName(), TimeUtil.getHHmmss(mRecordTime * 1000L), TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))));
        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(0, listSave);
            AppSpUtils.saveRecordList(listSp);
        } else {
            AppSpUtils.saveRecordList(listSave);
        }
    }

    //开始
    private void startRecordTime() {
        mRecordTime = 0;
        mHandler.postDelayed(mUpdateRecordTimeTask, 1000);
    }

    //结束
    private void stopRecordTime() {
        mHandler.removeCallbacks(mUpdateRecordTimeTask);
    }

}